/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame.game;

import java.io.IOException;
import cardgame.network.*;

/**
 *
 * @author Carl
 */
public class Card implements EventListener, BitSerializable{

    public Card(){
        description = new Description(getClass().getSimpleName());
    }
    protected Game game;
    private int id = -1;
    protected Player owner;
    protected CardPosition cardPosition = new CardPosition();
    protected Description description;
    protected Mana[] manaTypes = new Mana[]{Mana.CUSTOM};
    private boolean isTapped;

    @Override
    public boolean preEvent(Game game, Event receivedEvent){
        return true;
    }

    @Override
    public void postEvent(Game game, Event receivedEvent){
        
    }

    public void setGame(Game game){
        if(game != this.game){
            this.game = game;
            game.getGameMode().prepareCard(this);
            game.registerCard(this);
            game.addEventListener(this);
        }
    }

    public Game getGame(){
        return game;
    }

    public void setID(int id){
        this.id = id;
    }

    public int getID(){
        return id;
    }

    public Player getOwner(){
        return owner;
    }

    public void setOwner(Player owner){
        this.owner = owner;
        setGame(owner.getGame());
    }

    public void setCardPosition(CardPosition cardPosition){
        this.cardPosition = cardPosition;
    }

    public CardPosition getCardPosition(){
        return cardPosition;
    }

    public Description getDescription(){
        return description;
    }

    public Mana[] getManaTypes(){
        return manaTypes;
    }
    
    public void tap(){
        isTapped = true;
    }
    
    public void untap(){
        isTapped = false;
    }

    public boolean isTapped(){
        return isTapped;
    }
    
    @Override
    public Card clone(){
        Card card = CardTypeManager.createCard(getClass());
        byte[] data = NetworkUtil.serialize(this);
        NetworkUtil.unserialize(card, data);
        return card;
    }

    @Override
    public void write(BitOutputStream outputStream){
        outputStream.writeBits(id, 10);
        if(owner != null){
            outputStream.writeBoolean(true);
            outputStream.writeBits(owner.getIndex(), 1);
        }
        else{
            outputStream.writeBoolean(false);
            outputStream.writeBoolean(false);
        }
        outputStream.writeBoolean(isTapped);
    }

    @Override
    public void read(BitInputStream inputStream) throws IOException{
        id = inputStream.readBits(10);
        boolean hasOwner = inputStream.readBoolean();
        int ownerIndex = inputStream.readBits(1);
        if(hasOwner){
            owner = game.getPlayer(ownerIndex);
        }
        else{
            owner = null;
        }
        isTapped = inputStream.readBoolean();
    }
}
